#include "filter_plugin.h"

#include "ui_plugin.h"

#define BLUR_FILTER_PAGE                1
#define MEDIAN_FILTER_PAGE              2
#define GAUSSIAN_FILTER_PAGE            3
#define BILATERAL_FILTER_PAGE           4

Filter_Plugin::Filter_Plugin()
{
    // Insert initialization codes here ...
}

Filter_Plugin::~Filter_Plugin()
{
    // Insert cleanup codes here ...
}

QString Filter_Plugin::title()
{
    return this->metaObject()->className();
}

QString Filter_Plugin::version()
{
    return "1.0.0";
}

QString Filter_Plugin::description()
{
    return "Performs different filters available in OpenCV";
}

QString Filter_Plugin::help()
{
    return "This is a plugin that performs different filters available in OpenCV";
}

void Filter_Plugin::setupUi(QWidget *parent)
{
    ui = new Ui::PluginGui;
    ui->setupUi(parent);

    ui->Tabs->setCurrentIndex(0);
    connect(ui->Tabs, SIGNAL(currentChanged(int)), this, SLOT(on_Tabs_currentChanged(int)));

    connect(ui->blurKsizeWSpinBox, SIGNAL(valueChanged(int)), this, SLOT(on_blurKsizeWSpinBox_valueChanged(int)));
    connect(ui->blurKsizeHSpinBox, SIGNAL(valueChanged(int)), this, SLOT(on_blurKsizeHSpinBox_valueChanged(int)));
    connect(ui->blurAnchorXSpinBox, SIGNAL(valueChanged(int)), this, SLOT(on_blurAnchorXSpinBox_valueChanged(int)));
    connect(ui->blurAnchorYSpinBox, SIGNAL(valueChanged(int)), this, SLOT(on_blurAnchorYSpinBox_valueChanged(int)));

    connect(ui->medianKsizeSpinBox, SIGNAL(valueChanged(int)), this, SLOT(on_medianKsizeSpinBox_valueChanged(int)));

    connect(ui->gaussKsizeWspinBox, SIGNAL(valueChanged(int)), this, SLOT(on_gaussKsizeWspinBox_valueChanged(int)));
    connect(ui->gaussKsizeHspinBox, SIGNAL(valueChanged(int)), this, SLOT(on_gaussKsizeHspinBox_valueChanged(int)));
    connect(ui->gausssgXdoubleSpinBox, SIGNAL(valueChanged(double)), this, SLOT(on_gausssgXdoubleSpinBox_valueChanged(double)));
    connect(ui->gausssgYdoubleSpinBox, SIGNAL(valueChanged(double)), this, SLOT(on_gausssgYdoubleSpinBox_valueChanged(double)));

    connect(ui->bilatDSpinBox, SIGNAL(valueChanged(int)), this, SLOT(on_bilatDSpinBox_valueChanged(int)));
    connect(ui->bilatSigColoSpinBox, SIGNAL(valueChanged(double)), this, SLOT(on_bilatSigColoSpinBox_valueChanged(double)));
    connect(ui->bilatSigSpaceSpinBox, SIGNAL(valueChanged(double)), this, SLOT(on_bilatSigSpaceSpinBox_valueChanged(double)));

    connect(ui->urlLabel,SIGNAL(linkActivated(QString)),this,SLOT(openURL(QString)));

}

void Filter_Plugin::processImage(const cv::Mat &inputImage, cv::Mat &outputImage)
{
    using namespace cv;

    switch(ui->Tabs->currentIndex())
    {
        case BLUR_FILTER_PAGE:
            blur(inputImage,
                 outputImage,
                 Size(ui->blurKsizeWSpinBox->value(),
                      ui->blurKsizeHSpinBox->value()),
                 Point(ui->blurAnchorXSpinBox->value(),
                       ui->blurAnchorYSpinBox->value()));
            break;

        case MEDIAN_FILTER_PAGE:
            medianBlur(inputImage,
                       outputImage,
                       ui->medianKsizeSpinBox->value());
            break;

        case GAUSSIAN_FILTER_PAGE:
            GaussianBlur(inputImage,
                         outputImage,
                         Size(ui->gaussKsizeWspinBox->value(),
                              ui->gaussKsizeHspinBox->value()),
                         ui->gausssgXdoubleSpinBox->value(),
                         ui->gausssgYdoubleSpinBox->value());
            break;


        case BILATERAL_FILTER_PAGE:
            bilateralFilter(inputImage,
                            outputImage,
                            ui->bilatDSpinBox->value(),
                            ui->bilatSigColoSpinBox->value(),
                            ui->bilatSigSpaceSpinBox->value());
            break;

        default:
            break;
    }
}

void Filter_Plugin::openURL(QString url)
{
    QDesktopServices::openUrl(url);
}

void Filter_Plugin::on_blurKsizeWSpinBox_valueChanged(int arg1)
{
    Q_UNUSED(arg1);
    emit updateNeeded();
}

void Filter_Plugin::on_blurKsizeHSpinBox_valueChanged(int arg1)
{
    Q_UNUSED(arg1);
    emit updateNeeded();
}

void Filter_Plugin::on_blurAnchorXSpinBox_valueChanged(int arg1)
{
    Q_UNUSED(arg1);
    emit updateNeeded();
}

void Filter_Plugin::on_blurAnchorYSpinBox_valueChanged(int arg1)
{
    Q_UNUSED(arg1);
    emit updateNeeded();
}


void Filter_Plugin::on_gaussKsizeWspinBox_valueChanged(int arg1)
{
    Q_UNUSED(arg1);
    emit updateNeeded();
}

void Filter_Plugin::on_gaussKsizeHspinBox_valueChanged(int arg1)
{
    Q_UNUSED(arg1);
    emit updateNeeded();
}

void Filter_Plugin::on_gausssgXdoubleSpinBox_valueChanged(double arg1)
{
    Q_UNUSED(arg1);
    emit updateNeeded();
}

void Filter_Plugin::on_gausssgYdoubleSpinBox_valueChanged(double arg1)
{
    Q_UNUSED(arg1);
    emit updateNeeded();
}

void Filter_Plugin::on_bilatDSpinBox_valueChanged(int arg1)
{
    Q_UNUSED(arg1);
    emit updateNeeded();
}

void Filter_Plugin::on_bilatSigColoSpinBox_valueChanged(double arg1)
{
    Q_UNUSED(arg1);
    emit updateNeeded();
}

void Filter_Plugin::on_bilatSigSpaceSpinBox_valueChanged(double arg1)
{
    Q_UNUSED(arg1);
    emit updateNeeded();
}

void Filter_Plugin::on_medianKsizeSpinBox_valueChanged(int arg1)
{
    //if(arg1 % 2 == 1) // Must be odd
    Q_UNUSED(arg1);
    emit updateNeeded();
}

void Filter_Plugin::on_Tabs_currentChanged(int index)
{
    Q_UNUSED(index);
    emit updateNeeded();
}



